package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import lxt.action.core.ActionSupport;
import lxt.entity.Parameter;
import lxt.entity.TruckInfo;
import lxt.service.ParameterService;
import lxt.service.TruckService;
/**
 * 车辆管理action
 * @author Lit
 *
 */
public class TruckAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	//定义属性
	 private String truckId;
     private String truckBrand;
     private String truckModel;
     private String truckType;
     private String truckName;
     private double maxLoad;
     private String netryTime;
     private double netryFee;
     private String note;
	
     //定义service
	private TruckService truckService ;
	private ParameterService parameterService ;
	
	//定义对象
	private List<TruckInfo> trucks;
	private List<Parameter> paramter ;
	private TruckInfo truck ;

	//定义页面对象
	private String deleteIds;
	private String isHasFalg = "false";
	//定义分页变量
	private Integer pageCounts = 10; //每页显示数量
	private String currPageStr = "1"; //分页
	private Integer totalCounts ;//总数据量
	
	/**
	 * 加载
	 * @return
	 */
	public String loadAll(){
		try{
			//定义从第几条显示
			int offset =( Integer.parseInt(currPageStr)-1)*pageCounts;
			
			trucks = truckService.loadTruck(truckId, netryTime, offset, pageCounts);
			
			totalCounts = truckService.loadAllTruckCount(truckId, netryTime);
			
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加预处理
	 * @return
	 */
	public String AddPre(){
		try{
			paramter = parameterService.loadByParamType("truckType");
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 添加
	 */
	public String add(){
		try{
			//定义变量
			TruckInfo truck ;//= new TruckInfo();
			truck = this.truckService.loadTruckById(truckId);
			if(truck != null){
				this.addActionMessage("该车牌号已经存在,请重新添加.");
				paramter = parameterService.loadByParamType("truckType");
				return INPUT;
			}
			else{
				truck = new TruckInfo();
				
				truck.setTruckId(truckId);
				truck.setMaxLoad(maxLoad);
				truck.setNetryFee(netryFee);
				
				if(netryTime!=null&&!netryTime.equals("")){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
					Date date = sdf.parse(netryTime);
					truck.setNetryTime(date);
				}
				truck.setNote(note);
				truck.setTruckBrand(truckBrand);
				truck.setTruckModel(truckModel);
				truck.setTruckName(truckName);
				truck.setTruckType(truckType);
				
				this.truckService.addTruck(truck);
			}
			
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 编辑预处理
	 * @return
	 */
	public String updatePre(){
		try{
			paramter = parameterService.loadByParamType("truckType");
			
			truck = this.truckService.loadTruckById(truckId);
			//为页面属性赋值
			this.truckId = truck.getTruckId();
			this.netryFee = truck.getNetryFee();
			if(truck.getNetryTime()!=null){
				this.netryTime = truck.getNetryTime().toString();
			}
			this.truckBrand = truck.getTruckBrand();
			this.truckModel = truck.getTruckModel();
			this.truckName = truck.getTruckName();
			this.truckType = truck.getTruckType();
			this.maxLoad = truck.getMaxLoad();
			this.note = truck.getNote();
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 修改
	 * @return
	 */
	public String update(){
		try{
			//重新获取修改目标
			TruckInfo truck = this.truckService.loadTruckById(truckId);
			//更新目标对象的目标修改属性(部分属性)
			truck.setMaxLoad(maxLoad);
			truck.setNetryFee(netryFee);
			if(netryTime!=null&&!netryTime.equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
				Date date = sdf.parse(netryTime);
				truck.setNetryTime(date);
			}
			truck.setNote(note);
			truck.setTruckBrand(truckBrand);
			truck.setTruckModel(truckModel);
			truck.setTruckName(truckName);
			truck.setTruckType(truckType);
			
			truckService.updateTruck(truck);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * 删除
	 * @return
	 */
	public String delete(){
		try{
			truckService.deleteTruck(deleteIds);
		}catch(Exception ex){
			ex.printStackTrace();
			this.addActionMessage("操作失败，请检查重新操作");
		}
		return SUCCESS;
	}
	/**
	 * get set
	 */
	
	public TruckService getTruckService() {
		return truckService;
	}
	public void setTruckService(TruckService truckService) {
		this.truckService = truckService;
	}
	public String getTruckId() {
		return truckId;
	}
	public void setTruckId(String truckId) {
		System.out.println("id = " +truckId);
		this.truckId = truckId;
	}
	public String getTruckBrand() {
		return truckBrand;
	}
	public void setTruckBrand(String truckBrand) {
		this.truckBrand = truckBrand;
	}
	public String getTruckModel() {
		return truckModel;
	}
	public void setTruckModel(String truckModel) {
		this.truckModel = truckModel;
	}
	public String getTruckType() {
		return truckType;
	}
	public void setTruckType(String truckType) {
		this.truckType = truckType;
	}
	public String getTruckName() {
		return truckName;
	}
	public void setTruckName(String truckName) {
		this.truckName = truckName;
	}
	public double getMaxLoad() {
		return maxLoad;
	}
	public void setMaxLoad(double maxLoad) {
		this.maxLoad = maxLoad;
	}
	public String getNetryTime() {
		return netryTime;
	}
	public void setNetryTime(String netryTime) {
		this.netryTime = netryTime;
	}
	public double getNetryFee() {
		return netryFee;
	}
	public void setNetryFee(double netryFee) {
		this.netryFee = netryFee;
	}
	public String getNote() {
		return note;
	}
	public void setNote(String note) {
		this.note = note;
	}
	public String getDeleteIds() {
		return deleteIds;
	}
	public void setDeleteIds(String deleteIds) {
		this.deleteIds = deleteIds;
	}
	public Integer getPageCounts() {
		return pageCounts;
	}
	public void setPageCounts(Integer pageCounts) {
		this.pageCounts = pageCounts;
	}
	public String getCurrPageStr() {
		return currPageStr;
	}
	public void setCurrPageStr(String currPageStr) {
		this.currPageStr = currPageStr;
	}
	public Integer getTotalCounts() {
		return totalCounts;
	}
	public void setTotalCounts(Integer totalCounts) {
		this.totalCounts = totalCounts;
	}
	public List<TruckInfo> getTrucks() {
		return trucks;
	}
	public void setTrucks(List<TruckInfo> trucks) {
		this.trucks = trucks;
	}
	public TruckInfo getTruck() {
		return truck;
	}
	public void setTruck(TruckInfo truck) {
		this.truck = truck;
	}
	public ParameterService getParameterService() {
		return parameterService;
	}
	public void setParameterService(ParameterService parameterService) {
		this.parameterService = parameterService;
	}
	public List<Parameter> getParamter() {
		return paramter;
	}
	public void setParamter(List<Parameter> paramter) {
		this.paramter = paramter;
	}
	public String getIsHasFalg() {
		return isHasFalg;
	}
	public void setIsHasFalg(String isHasFalg) {
		this.isHasFalg = isHasFalg;
	}

}
